package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.orderaddress.ManageAddressActivity;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.CircleImageView;
import com.example.jbmsuppliers.sinopec.view.MyLog;
import com.example.weather.myframe.PopupSelectImage;
import com.jbm.jbmsupplier.util.ICtrl;
import com.jbm.jbmsupplier.util.LoginEntity;
import com.squareup.picasso.Picasso;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.jbm.jbmsupplier.util.abstracts.SendMessageInterfase.gSocket;

/**
 * Created by 家帮忙 on 2017/2/27.
 */

public class MyUserSet extends ActivityUse {
    @InjectView(R.id.usersetxiang)
    CircleImageView usersetxiang;
    @InjectView(R.id.textview_lznicheng)
    TextView textviewLznicheng;
    @InjectView(R.id.layout_lzsex)
    TextView layoutLzsex;
    @InjectView(R.id.layout_lzaddress)
    TextView layoutLzaddress;
    @InjectView(R.id.layout_lztouxiang)
    LinearLayout layoutLztouxiang;
    @InjectView(R.id.layout_lztouxiang1)
    LinearLayout layoutLztouxiang1;
    @InjectView(R.id.layout_lztouxiang2)
    LinearLayout layoutLztouxiang2;
    @InjectView(R.id.layout_lztouxiang3)
    LinearLayout layoutLztouxiang3;
    @InjectView(R.id.header_back)
    ImageView headerBack;
    @InjectView(R.id.header_title)
    TextView headerTitle;
    @InjectView(R.id.header_right)
    TextView headerRight;
    @InjectView(R.id.view1)
    View view1;
    @InjectView(R.id.view3)
    View view3;
    @InjectView(R.id.view4)
    View view4;
    private SharedPreferences mSharedPreferences;
    private final int USETSETMESSAGE = 0001;
    private final int USERTOUXIANG = 0002;
    private final int USERNICHENG = 0003;
    private final int USERADDRESS = 0004;
    private final int CHANGIMAGE = 0005;
    private final int CHANGNAME = 0006;
    private final int CHANGSEX = 0007;
    private final int MODIHEAD = 00010;
    private final int UPDATEMES = 00011;
    private SharedPreferences.Editor urledit;

    @Override
    protected int setContentView1() {
        return R.layout.lz_usersetactivity;
    }

    @Override
    protected void initView() {
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);

         headerTitle.setText("修改信息");
         headerRight.setVisibility(View.GONE);

        mSharedPreferences = getSharedPreferences("sinopecall",
                Activity.MODE_PRIVATE);
        urledit = mSharedPreferences.edit();
        handler.sendMessage(handler.obtainMessage(USETSETMESSAGE));

    }

    @Override
    protected void onStart() {
        NetWork netWork = new NetWork(handler);
//        if (mSharedPreferences.getInt("result.login_id",0)==0){
            netWork.setUpdateMessage(gSocket.login_id, UPDATEMES);
//        }else {
//            netWork.setUpdateMessage(mSharedPreferences.getInt("result.login_id", 0), UPDATEMES);
//
//        }


        super.onStart();
    }

    //填写地址信息
    private void AddRessMessage() {
        startActivity(new Intent(this, ManageAddressActivity.class));
    }

    //修改头像的方法
    private void setChangImage() {


        Intent intent = new Intent(MyUserSet.this, PopupSelectImage.class);
        startActivityForResult(intent, 100);


    }

    //修改昵称的方法
    private void setChangName() {
        startActivity(new Intent(this, MyNameChang.class));
    }

    //修改性别的方法
    private void setChangSex() {
        startActivity(new Intent(this, MySexChang.class));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK)
            return;
        if (requestCode == 100) {
            File file = (File) data.getSerializableExtra("file");
            Picasso.with(MyUserSet.this).load(file).into(usersetxiang);
            byte[] result = getFileToByte(file);//转换成功了
            NetWork netWork = new NetWork(handler);
//            if (mSharedPreferences.getInt("result.login_id",0)==0){
                netWork.setModiHead(gSocket.login_id, result, getPhotoFileName(), MODIHEAD);
//            }else {
//                netWork.setModiHead(mSharedPreferences.getInt("result.login_id", 0), result, getPhotoFileName(), MODIHEAD);

//            }

        }
    }

    public byte[] getFileToByte(File file) {
        byte[] by = new byte[(int) file.length()];
        try {
            InputStream is = new FileInputStream(file);
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            byte[] bb = new byte[2048];
            int ch;
            ch = is.read(bb);
            while (ch != -1) {
                bytestream.write(bb, 0, ch);
                ch = is.read(bb);
            }
            by = bytestream.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return by;
    }

    private String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'yyyyMMddHHmmssSSS");
        return dateFormat.format(date) + ".png";
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case USETSETMESSAGE: {
                    String url = mSharedPreferences.getString("headumgur", "");
                    int sex = mSharedPreferences.getInt("sex", 0);
                    String nickname = mSharedPreferences.getString("nickname", "");
                    String weixin = mSharedPreferences.getString("weinxinlogin", "");
                    if (weixin.equals("weixin")) {
                        if (gSocket.logo.equals("")) {

                        } else {
                            Picasso.with(MyUserSet.this).load(gSocket.logo).into(usersetxiang);
                        }
                        if (gSocket.nickname.equals("")) {

                        } else {
                            textviewLznicheng.setText(gSocket.nickname);
                        }
                        if (gSocket.sex > 0) {
                            if (gSocket.sex == 1) {
                                layoutLzsex.setText("男");
                            } else if (gSocket.sex == 2) {
                                layoutLzsex.setText("女");
                            }
                        }

                    } else if (weixin.equals("shouji")) {
                        if (gSocket.logo != null) {
                            if (gSocket.logo.length() > 5) {
                                MyLog.e(gSocket.logo);
                                Picasso.with(MyUserSet.this).load(gSocket.logo).into(usersetxiang);
                            } else {
                            }
                        } else {

                        }
                        if (gSocket.nickname != null) {
                            textviewLznicheng.setText(gSocket.nickname);
                        } else {

                        }
                        if (gSocket.sex == 1) {
                            layoutLzsex.setText("男");
                        } else if (gSocket.sex == 2) {
                            layoutLzsex.setText("女");
                        }

                    }
                    break;
                }
                case USERADDRESS: {
                    AddRessMessage();
                    break;
                }
                case CHANGIMAGE: {
                    setChangImage();
                    break;
                }
                case CHANGNAME: {
                    setChangName();
                    break;
                }
                case CHANGSEX: {
                    setChangSex();
                    break;
                }
                case MODIHEAD: {
                    ICtrl busin = (ICtrl) msg.obj;
                    if (busin.back) {
//                        finish();

                    } else {
                        hint(busin.errorinfo);
                    }


                    break;
                }
                case UPDATEMES: {
                    LoginEntity busin = (LoginEntity) msg.obj;
                    if (busin.back) {
                        if (busin.getLogo().equals("")) {
                        } else {
                            Picasso.with(MyUserSet.this).load(busin.getLogo()).into(usersetxiang);
                            urledit.putString("urlnames", busin.getLogo());
                        }
                        if (busin.getNickname().equals("")) {
                        } else if (!busin.getNickname().equals("")) {
                            textviewLznicheng.setText(busin.getNickname());
                        }
                        if (busin.getAccSecretEntity().getSex() == null) {
                            return;
                        } else if (busin.getAccSecretEntity().getSex().equals("")) {
                            return;
                        } else if (busin.getAccSecretEntity().getSex() == 1) {
                            layoutLzsex.setText("男");
                        } else if (busin.getAccSecretEntity().getSex() == 2) {
                            layoutLzsex.setText("女");
                        }
                    } else {
                        try {
                            return;
                        } catch (Exception e) {
                            return;
                        }
//                        hint(busin.errorinfo);
                    }


                    break;
                }

            }
        }
    };


    @OnClick({R.id.layout_lztouxiang, R.id.layout_lztouxiang1, R.id.layout_lztouxiang2, R.id.layout_lztouxiang3,R.id.header_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_lztouxiang:
                handler.sendMessage(handler.obtainMessage(CHANGIMAGE));
                break;
            case R.id.layout_lztouxiang1:
                handler.sendMessage(handler.obtainMessage(CHANGNAME));
                break;
            case R.id.layout_lztouxiang2:
                handler.sendMessage(handler.obtainMessage(CHANGSEX));
                break;
            case R.id.layout_lztouxiang3:
                handler.sendMessage(handler.obtainMessage(USERADDRESS));
                break;
            case R.id.header_back:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        NetWork netWork=new NetWork(handler);
//        netWork.setUpdateMessage(gSocket.login_id,UPDATEMES);


    }

}
